#ifndef __GTC_PROTOCOL_H__
#define __GTC_PROTOCOL_H__

#include "typedef.h"
#include "platform.h"
#include "gtc_client_struct.h"

#define  ECO_FALSE   0
#define  ECO_TRUE    1

#define MQTT_SENT_QUEUE_ITEM_LEN   (500u)
#define MQTT_SENT_QUEUE_NUM   (4u)

enum GtcMqttCmdType{
    gtc_cmd_sign_in = 1,
    gtc_cmd_status,
    gtc_cmd_bms,
    gtc_cmd_bill_charging,  //充电中账单数据
	gtc_cmd_card_auth,
	gtc_cmd_start_charge_ack,
    gtc_cmd_stop_charge_ack,
    gtc_cmd_update_cmd_ack,


};

struct GtcSignInAck {
    uint32_t time;
};

struct GtcSwipCardAck {
    uint32_t gunid;
    char_t card_id[GTC_MAX_CHAEGE_USER_ID_LEN+1];
    char_t dev_owner[GTC_MAX_CHAEGE_USER_ID_LEN+1];
    char_t card_owner[GTC_MAX_CHAEGE_USER_ID_LEN+1];
    uint8_t ret;
    char_t user_id[GTC_MAX_CHAEGE_USER_ID_LEN+1];
    char_t dno[GTC_MAX_CHAEGE_USER_ID_LEN+1];
    uint8_t type;
    uint32_t value;
};

struct GtcServerStartMsg {
    uint32_t gunid;
    char_t userId[GTC_MAX_CHAEGE_USER_ID_LEN+1];
    char_t dno[GTC_MAX_CHAEGE_USER_ID_LEN+1];
    uint8_t type;
    uint32_t value;
};

struct GtcSettleAckMsg{
    uint32_t gunid;
    char_t userId[GTC_MAX_CHAEGE_USER_ID_LEN+1];
    char_t dno[GTC_MAX_CHAEGE_USER_ID_LEN+1];
    uint32_t charge_sec;
    uint32_t charge_ah;
    uint32_t charge_kwh;
    uint32_t elec_cost;
    uint32_t serv_cost;
    uint32_t total_cost;
};

struct GtcServerStopMsg{
    uint32_t gunid;
    char_t userId[GTC_MAX_CHAEGE_USER_ID_LEN+1];
    char_t dno[GTC_MAX_CHAEGE_USER_ID_LEN+1];    
};

struct GtcUpdateCmdRecMsg {
    char_t sno[17];
    char_t type[8];
    char_t sver[16];
    char_t name[16];
    uint32_t tlen;
    char_t time[32];
};

enum E_UpdateState{
    UPDATE_LOADING = 1,   //正在从服务器下载升级文件
    UPDATE_LOADED     //下载文件完成
};

//远程升级处理信息
struct T_updateInfo {
    char_t status;  //状态0 不升级，1 下载包中 2 包已经下载完成 3 已经更新
    char_t sno[17];
    char_t ver[16];
    char_t name[16];
    uint32_t totalLen;
    uint32_t num;
    uint32_t packetLen;
    uint32_t cnt;
    char_t ret[12];
    uint32_t recvLen;
    char_t bak[28];
};

//平台下发升级第一包
struct T_UpdateCmdRecMsg {
    char_t sno[17];
    char_t type[8];
    char_t sver[16];
    char_t name[16];
    uint32_t tlen;
    char_t time[32];
};

//单包数据结构,平台应答
struct T_UpdateFileRecMsg {
    char_t sno[17];
    char_t sver[16];
    char_t name[16];
    uint32_t num;
    uint32_t crc;
    uint32_t len;
};

enum CostMode{
    COST_MODE_SINGLE = 0,
    COST_MODE_TZONE,
};

struct GtcCostModeMsg{
    struct GtcFeeConfig fee; //分时段电费和服务费单价
};

struct GtcParaSetMsg{
    
};


struct GtcServerOpenMsgAck{

};


struct GtcRecvProtBase{
	struct GtcSignInAck	sign_in_ack;
	uint8_t	recv_sign_in_ack_flag;
    struct GtcSwipCardAck swip_card_ack;
    uint8_t swip_card_ack_flag;
    struct GtcSettleAckMsg settle_ack;
    uint8_t settle_ack_flag;

};

struct MqttSendMsg{
    uint8_t qos;
    char_t data[MQTT_SENT_QUEUE_ITEM_LEN];
};


void gtc_mqtt_sycle(void);
void gtc_cycle_task(void *para);
int32_t mqtt_send_msg(struct MqttSendMsg *msg);
int32_t mqtt_connect(void);
int32_t mqtt_is_connect(void);

struct GtcParams;
int32_t mqtt_init(struct GtcParams *params);

void gtc_mqtt_send_resp(enum GtcMqttCmdType cmd_type, void * cmd, uint8_t Qos);


#endif
